package com.hundsun.s3.client.service.imp;

import com.hundsun.jresplus.middleware.BaseBizRuntimeException;
import com.hundsun.s3.common.dto.DeptDTO;
import com.hundsun.s3.common.dto.Client;
import com.hundsun.s3.client.pub.common.BaseDao;
import com.hundsun.s3.pub.utils.BizSecurity;
import com.hundsun.s3.client.service.ClientService;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by xiaowx23615 on 2018/2/2.
 */
@Service
public class ClientServiceImp extends BaseDao implements ClientService {

    /**
     * 登陆
     * @param username
     * @param password
     * @return
     */
    public Client login(String username, String password) {
        Map<String,Object> map = new HashMap<String, Object>();
        map.put("username",username);
        map.put("password",password);
        Client client = null;
        try {
            client = this.get("800003",map,Client.class);
        } catch (BaseBizRuntimeException e) {
            return null;
        }

        return client;
    }

    /**
     * 注册 成功返回1，失败返回0
     * @param
     * @return
     */
    public String register(String username,String password,int sex,
                        String nickname,String mailbox,String birthday,String phonenumber,String dept_tree) {
        Map<String,Object> map = new HashMap<String,Object>();
        map.put("username",username);
        map.put("password",password);
        map.put("sex",sex);
        map.put("nickname",nickname);
        map.put("mailbox",mailbox);
        map.put("birthday",Integer.parseInt(birthday.replace("-","")));
        map.put("phonenumber",phonenumber);
        if(dept_tree == null || dept_tree.equals("") || dept_tree.equals("null")) dept_tree = "1";
        map.put("dept",dept_tree);
        String result = this.exc("800001",map);
        if("执行成功".equals(result)) {
            return "1";
        }else {
            return result;
        }
    }


//    public String findPwd(String username, String mailbox, String newPassword) {
//        Map<String,Object> map = new HashMap<>();
//        map.put("username",username);
//        map.put("mailbox",mailbox);
//        map.put("password",newPassword);
//        String result = this.exc("800004",map);
//        if("执行成功".equals(result)) {
//            return "1";
//        }else {
//            return result;
//        }
//    }

    /**
     * 更新
     * @param username
     * @param nickname
     * @param phonenumber
     * @param dept_tree
     * @return
     */
    public String updateUserInfo(String username, String nickname, String phonenumber, String dept_tree) {
        Map<String,Object> map = new HashMap<String, Object>();
        map.put("username",username);
        map.put("nickname",nickname);
        map.put("phonenumber",phonenumber);
        if(dept_tree == null || dept_tree.trim().equals("") || dept_tree.equals("null")) dept_tree = "1";
        map.put("dept",dept_tree);
        String resultMsg = this.exc("800008",map);
        if("执行成功".equals(resultMsg)) {
            return "1";
        }else {
            return resultMsg;
        }
    }

    /**
     * 判断用户是否存在
     * @param username
     * @return
     */
    public int judgeUsername(String username) {
        Map<String,Object> map = new HashMap<String, Object>();
        map.put("username",username);
        String resultMsg = this.exc("800009",map);
        return Integer.parseInt(resultMsg);
    }

    /**
     * 验证邮箱是否已经注册
     * @param mailbox
     * @return
     */
    public int judgeMailbox(String mailbox) {
        Map<String,Object> map = new HashMap<String, Object>();
        map.put("mailbox",mailbox);
        String resultMsg = this.exc("800013",map);
        return Integer.parseInt(resultMsg);
    }

    /**
     * 判断用户是否已经注销
     * @param username
     * @return
     */
    public int judgeUsernameCancle(String username) {
        Map<String,Object> map = new HashMap<String, Object>();
        map.put("username",username);
        String resultMsg = this.exc("800014",map);
        try {
            Integer.parseInt(resultMsg);
        }catch (Exception e) {
            return 0;
        }
        return Integer.parseInt(this.exc("800014",map));
    }

    /**
     * 获取部门集合
     * @return
     */
    public List<DeptDTO> getDeptList() {
        return this.qry("800017",new HashMap<String, Object>(),DeptDTO.class);
    }

    /**
     * 根据用户名获取用户
     * @param username/
     * @return
     */
    public Client getClientByUsername(String username) {
        Map<String,Object> map = new HashMap<String, Object>();
        map.put("username",username);
        Client client = null;
        try {
           client = this.get("800023",map,Client.class);
        }catch (Exception e) {
            return null;
        }
        return client;
    }







    /**
     * 修改密码
     * @param username
     * @param oldPwd
     * @param newPwd
     * @return
     */
    public String modifyPassword(String username, String oldPwd, String newPwd) {
        Map<String,Object> map = new HashMap<String, Object>();
        oldPwd = BizSecurity.desEncrypt(oldPwd, BizSecurity.key1, BizSecurity.key2, BizSecurity.key3);
        newPwd = BizSecurity.desEncrypt(newPwd, BizSecurity.key1, BizSecurity.key2, BizSecurity.key3);
        map.put("username",username);
        map.put("password",newPwd);
        Client client = login(username,oldPwd);
        String resultMsg = null;
        if (client == null) {
            resultMsg = "密码错误";
            return resultMsg;
        }
        resultMsg = this.exc("800004",map);
        if ("执行成功".equals(resultMsg)) {
            return "1";
        }
        return resultMsg;
    }


}
